package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.query.LandmarkDetailComQueryObject;
import cn.wolfcode.trip.base.query.LtCatalogDetailQueryObject;
import cn.wolfcode.trip.base.service.ILandmarkDetaiService;
import cn.wolfcode.trip.base.service.ILandmarkDetailCommendService;
import cn.wolfcode.trip.base.service.ILmTicketCatalogBigService;
import com.github.pagehelper.PageInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("lmTicketCatalogBigs")
public class lmTicketCatalogBigController {
    @Autowired
    private ILmTicketCatalogBigService lmTicketCatalogBigService;
    @Autowired
    private ILandmarkDetaiService landmarkDetaiService;
    @Autowired
    private ILandmarkDetailCommendService landmarkDetailCommendService;


    //推荐页面

    //1.推荐以及大标题
    @GetMapping("/load")
    public Object loadRegionByState(int state){
        return lmTicketCatalogBigService.getLmTicketCatalogBigByState(state);
    }
    //2.折扣相关景点门票
    @GetMapping("/{state}")
    public Object getLmDetailByState(@PathVariable long state){
        return landmarkDetaiService.getLmDetailByState(state);
    }
    //3.专题推荐
    @GetMapping("/special")
    public Object getSpecialCommendByState(LtCatalogDetailQueryObject qo){
        return lmTicketCatalogBigService.getSpecialCommendByState(qo);
    }



    //地区下攻略分页相关
    @GetMapping(value = "/{cbId}/childs",produces = "application/json;charset=utf-8")
    public Object listStrategiesJSON(LtCatalogDetailQueryObject qo) throws Exception {
        return  lmTicketCatalogBigService.query(qo);
    }


    //其他非推荐分类
    @Autowired
    private ServletContext ctx;

    @GetMapping(value = "/{cbId}/childs",produces = "text/html;charset=utf-8")
    public void listStrategiesHTML(@PathVariable int cbId, HttpServletResponse response) throws Exception {
        LtCatalogDetailQueryObject qo1=new LtCatalogDetailQueryObject();
        //全部分类
        qo1.setParentId(cbId);
        qo1.setPageSize(8);
        PageInfo all = lmTicketCatalogBigService.query(qo1);

        //本类推荐门票
        LandmarkDetailComQueryObject qo2=new LandmarkDetailComQueryObject();
        qo2.setPageSize(0);
        qo2.setCbId(cbId);
        PageInfo commends = landmarkDetailCommendService.getCommendByCbId(qo2);

        //freemarker  模板+数据=输出
        Configuration cfg=new Configuration(Configuration.VERSION_2_3_28);
        cfg.setDefaultEncoding("utf-8");
        cfg.setDirectoryForTemplateLoading(new File(ctx.getRealPath("/template")));
        Template template = cfg.getTemplate("pushLtCatalog.ftl");

        Map<String,Object> data=new HashMap<String, Object>();
        data.put("commends",commends);
        data.put("all",all);
        template.process(data,response.getWriter());
    }
}
